layui.use(function() {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var util = layui.util;
    let http_yzm = ''

    // 自定义验证规则
    form.verify({
        // 确认密码
        confirmPassword: function(value, item) {
            var passwordValue = $('#reg-password').val();
            if (value !== passwordValue) {
                return '两次密码输入不一致';
            }
        },

        //确认验证码
        confirmVerification_code: function(value, item) {
            if (value != http_yzm) {
                return '验证码错误';
            }
        }
    });

    // 提交事件
    form.on('submit(demo-reg)', function(data) {
        var field = data.field; // 获取表单字段值
        console.log(field.nickname);
        console.log(field.password);

        //登录接口
        jQuery.ajax({
                type: 'post',
                url: 'http://127.0.0.1:3000/users/login',
                data: {
                    name: field.nickname,
                    pwd: field.password,
                },
                success: result => {
                    console.log(result);
                    console.log();
                    if (result.code == 200) {
                        alert('登陆成功')
                        location.assign("./小项目.html")
                    } else if (result.code == -1) {
                        alert('用户名或密码错误')
                    }
                },
                error: e => {
                    console.log(e);
                }
            })
            // 阻止默认 form 跳转
        return false;
    });

    let yzm = jQuery('.Verification_code')
        // 普通事件
    util.on('lay-on', {
        // 获取验证码
        'reg-get-vercode': function(othis) {
            var isvalid = form.validate('#reg-cellphone'); // 主动触发验证，v2.7.0 新增 
            // 验证通过
            function Verification_code() {
                jQuery.ajax({
                    type: 'get',
                    url: 'http://47.94.4.201/index.php/index/index/getcode',
                    dataType: 'json',
                    success: s => {
                        yzm.text(s.msg)
                        http_yzm = s.msg
                    },
                    error: e => {
                        console.log(e);
                    }
                })
            }
            Verification_code()
        }
    });
});
//切换到注册
jQuery('.register_switch').click(() => {
    location.assign("./注册.html")
})